package com.rong.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rong.ruiji.common.BaseContext;
import com.rong.ruiji.common.R;
import com.rong.ruiji.entity.Employee;
import com.rong.ruiji.service.EmployeeService;
import com.rong.ruiji.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService ;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //将页面提交的密码进行MD5加密
        String password =   employee.getPassword();
        if(password == null) {
            return R.error("密码不能为空");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);



        //如果没有查询到
        if(emp == null) {
            return R.error("用户名不存在");
        }

        //密码比对
        if(!emp.getPassword().equals(password)) {
            return  R.error("用户名或密码错误");
        }

        //查看员工状态
        if(emp.getStatus() == 0) {
            return R.error("账号已被封禁");
        }

        //登录成功,将员工存入session
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp) ;
    }

     @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
         //清理session中的员工id
         request.getSession().removeAttribute("employee");
        return  R.success("退出成功") ;
     }


    /**
     * 新增员工
     * @param employee
     * @return
     */
     @PostMapping
    public  R<String> add(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("新增员工，员工信息：{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Long empId =(Long) (request.getSession().getAttribute("employee"));
        BaseContext.setCurrentId(empId);
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增成功");
     }

    /**
     * 员工信息分页查询
     * @return
     */
     @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {
         log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

         //构造分页构造器
         Page pageInfo = new Page(page,pageSize);

         //构造条件构造器
         LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
         queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
         //添加排序条件
         queryWrapper.orderByDesc(Employee::getName);
         //执行查询
         employeeService.page(pageInfo,queryWrapper);

         return R.success(pageInfo);
     }


    /**
     * 更新员工信息
     * @param employee
     * @return
     */
     @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {
            log.info(employee.toString());

            Long emp = (Long) request.getSession().getAttribute("employee");
//            employee.setUpdateTime(LocalDateTime.now());
//            employee.setUpdateUser(emp);
         BaseContext.setCurrentId(emp);
            employeeService.updateById(employee);
            return R.success("员工信息修改成功");
     }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
     @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id) {
         Employee employee = employeeService.getById(id);
         if(employee!= null) {
             return R.success(employee);
         }
            return R.error("没有查询到对应员工信息");
     }


}
